#include <butil/logging.h>
#include<brpc/channel.h>
#include<memory>
#include<iostream>
#include "main.pb.h"
#include <thread>

void callback(brpc::Controller* cntl, ::demo::EchoResponse* response) {
    std::unique_ptr<brpc::Controller> cntl_guard(cntl);
    std::unique_ptr<demo::EchoResponse> resp_guard(response);
    if (cntl->Failed() == true) {
        std::cout << "Rpc调用失败：" << cntl->ErrorText() << std::endl;
        return;
    }
    std::cout << "收到响应: " << response->msg() << std::endl;
}



int main()
{
    brpc::ChannelOptions options;
    options.connect_timeout_ms = -1;
    options.timeout_ms = -1;
    options.max_retry = 3;
    options.protocol = "baidu_std";

    brpc::Channel channel;
    channel.Init("127.0.0.1:8080",&options);

    demo::EchoService_Stub stub(&channel);
    demo::EchoRequest req;
    req.set_msg("你好，徐");
    auto rsp = new demo::EchoResponse();
    //auto rsp = new demo::EchoResponse();
    brpc::Controller *cntl = new brpc::Controller();
    auto clusure = google::protobuf::NewCallback(callback, cntl, rsp);
    stub.Echo(cntl,&req,rsp,clusure);
    // stub.Echo(cntl,&req,rsp,nullptr);
    // if(!cntl->Failed())
    // {
    //     std::cout<<rsp->msg()<<std::endl;
    // }

     std::this_thread::sleep_for(std::chrono::seconds(10));
    return 0;
}